package ca.uvic.cs.cogz.filters;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import edu.stanford.smi.protege.util.ComponentFactory;
import edu.stanford.smi.protegex.prompt.KnowledgeBaseInMerging;
import edu.stanford.smi.protegex.prompt.plugin.util.KnowledgeBaseFacade;
import edu.stanford.smi.protegex.prompt.ui.SourceClsesPane;
import edu.stanford.smi.protegex.prompt.ui.SourceFramesPane;

/**
 * Manages the hierarchy filter dialog that allows a user to filter candidate mappings based on nodes 
 * in the ontologies.
 * 
 * @tag critial : should fix this so it does not depend on internal Prompt code
 * @author seanf
 */
public class HierarchyFilter extends JPanel {
	private static final long serialVersionUID = 1L;

	private static boolean ALLOW_SELECTION = true;
	
	/** an action item that is activated when the user clicks the filter button */
	private FilterPromptAction filterPromptAction;
	
	/** the source and target ontology frames */
	private SourceFramesPane sourceFramePane = null;	
	private SourceFramesPane targetFramePane = null;
	
	/** the preferred size of this display */
	private Dimension size;

	public HierarchyFilter(Dimension size, FilterPromptAction filterPromptAction) {
		this.size = size;
		this.filterPromptAction = filterPromptAction;
		
		this.setPreferredSize(size);
		this.setSize(size);
		this.setLayout(new BorderLayout());
		
		initialize();
	}

	private void initialize() {
		JPanel clsesPanel = ComponentFactory.createPanel();
		clsesPanel.setLayout(new BorderLayout());
		clsesPanel.add(createClsesPane(), BorderLayout.CENTER);
		add(clsesPanel, BorderLayout.CENTER);
		
		JPanel buttonPanel = ComponentFactory.createPanel();
		JButton filterButton = new JButton("Filter on selected");
		JButton cancelButton = new JButton("Cancel");
		
		filterButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(java.awt.event.MouseEvent evt) {
				filterPressed(evt);
			}
		});
		
		cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(java.awt.event.MouseEvent evt) {
				filterPromptAction.filterOnFrames(null, null, true);
			}
		});
		
		buttonPanel.add(filterButton);
		buttonPanel.add(cancelButton);
		
		add(buttonPanel, BorderLayout.SOUTH);
	}
	
	private void filterPressed(java.awt.event.MouseEvent evt) {		
		if(sourceFramePane != null && targetFramePane != null) {
			filterPromptAction.filterOnFrames(sourceFramePane.getSelection(), targetFramePane.getSelection(), true);
		}
	}

	private JComponent createClsesPane() {
		KnowledgeBaseInMerging sourceKnowledgeBase = KnowledgeBaseFacade.getMappingSourceKnowledgeBaseInMerging();
		KnowledgeBaseInMerging targetKnowledgeBase = KnowledgeBaseFacade.getMappingTargetKnowledgeBaseInMerging();
		
		try {
			sourceFramePane = new SourceClsesPane(size,
						sourceKnowledgeBase, new Boolean(ALLOW_SELECTION), new Boolean(true));
			
			targetFramePane = new SourceClsesPane(size,
					targetKnowledgeBase, new Boolean(ALLOW_SELECTION), new Boolean(false));
			
			JSplitPane sourcesSplitPane = ComponentFactory.createLeftRightSplitPane(false);
			sourcesSplitPane.setLeftComponent(sourceFramePane);
			sourcesSplitPane.setRightComponent(targetFramePane);
			sourcesSplitPane.setDividerLocation(size.width / 2);
			
			return sourcesSplitPane;
		} 
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
